<template>
  <div class="login-container">
    <el-card class="login-card" shadow="always">
      <template #header>
        <div class="login-header">
          <el-icon><Setting /></el-icon>
          <span>管理员登录</span>
        </div>
      </template>
      
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-position="top">
        <el-form-item label="管理员账号" prop="accountNumber">
          <el-input 
            v-model="loginForm.accountNumber" 
            placeholder="请输入管理员账号"
            :prefix-icon="User"
            clearable>
          </el-input>
        </el-form-item>
        
        <el-form-item label="密码" prop="adminPassword">
          <el-input 
            v-model="loginForm.adminPassword" 
            type="password" 
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
            clearable>
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="handleLogin" :loading="loading">
            登 录
          </el-button>
        </el-form-item>
        
        <div class="login-footer">
          <router-link to="/login" class="link">学生登录</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Setting } from '@element-plus/icons-vue'
import api from '@/api'

const router = useRouter()
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  accountNumber: '',
  adminPassword: ''
})

const rules = {
  accountNumber: [
    { required: true, message: '请输入管理员账号', trigger: 'blur' }
  ],
  adminPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      api.adminLogin(loginForm).then(res => {
        loading.value = false
        if (res.status_code === 1) {
          ElMessage.success('登录成功')
          sessionStorage.setItem('adminInfo', JSON.stringify(res.data))
          router.push('/admin/idle')
        } else {
          ElMessage.error(res.msg || '登录失败')
        }
      }).catch(() => {
        loading.value = false
      })
    }
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('@/assets/img/admin-login-back.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-card {
  width: 400px;
  border-radius: 12px;
}

.login-header {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
  color: #8B5CF6;
  gap: 8px;
}

.login-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}

.link {
  color: #8B5CF6;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
}

.link:hover {
  color: #a78bfa;
}
</style>
